<?php
/**
 * log_info表model
 * 
 * @author sz 2016-9-12 10:10:05
 * @version 1.0
 */
namespace Home\Model;
use Think\Model;

class LogInfoModel extends Model
{

    /**
     * model对应表和数据库信息
     *
     * @author sz 2016-9-12 10:10:11
     * @version 1.0
     */
    function __construct ()
    {
        parent::__construct('log_info', null, 'HAICHUANG');
    }

    /**
     * getLog
     *
     * @param            
     *
     * @return 获取日志信息
     * @author sz 2016-9-12 10:10:28
     * @version 1.0
     */
    public function getLog ($user_name, $start_time, $end_time, $page)
    {
        $sql = "SELECT l.id,l.user_id,m.user_name,m.real_name,m.group_id,g.name AS group_name,l.behavior,l.create_time 
	               FROM log_info l 
	               LEFT JOIN member_user m ON m.id=l.user_id 
                   LEFT JOIN group_info g ON g.id=m.group_id
	               WHERE 1=1";
        if ($user_name != null)
        {
            $sql .= " AND m.real_name like '%$user_name%'";
        }
        if ($start_time != null)
        {
            $start_time = $start_time . " 00:00:00";
            $sql .= " AND l.create_time >= '$start_time'";
        }
        if ($end_time != null)
        {
            $end_time = $end_time . " 23:59:59";
            $sql .= " AND l.create_time <= '$end_time'";
        }
        $sql .= " ORDER BY l.create_time DESC";
        if ($page != null)
        {
            $sql .= " LIMIT $page->firstRow, $page->listRows";
        }
        
        return $this->query($sql);
    }

    /**
     * 插入一条操作日志
     *
     * @param string $user_id
     *            指定的账号
     * @param string $template
     *            操作类型：ADD，DEL，MOD
     * @param string $behavior
     *            操作内容，主要存储操作内容
     * @return 插入成功，返回插入数据ID；插入失败或异常返回false
     * @author sz 2016-9-12 10:12:28
     * @version 1.0
     */
    public function addLog ($user_id, $template, $behavior)
    {
        $logArray = array();
        $logArray['user_id'] = $user_id;
        $logArray['template'] = $template;
        $logArray['behavior'] = $behavior;
        $logArray['create_time'] = array(
                'exp',
                'now()'
        );
        
        return $this->add($logArray);
    }   
}
?>